package cn.ly.design.action.clazz;

public class ChainTest {
	public static void main(String[] args) {
		MyHandler1 h1 = new MyHandler1();
		MyHandler2 h2 = new MyHandler2();
		MyHandler3 h3 = new MyHandler3();
		h1.setHandler(h2);
		h2.setHandler(h3);
		h1.operate("request");
	}
}

interface Handler{
	void operate(String request);
}


abstract class AbstractHandler{
	private Handler handler;
	public Handler getHandler(){
		return this.handler;
	}
	public void setHandler(Handler handler) {
		this.handler = handler;
	}
}

class MyHandler1 extends AbstractHandler implements Handler{

	@Override
	public void operate(String request) {
		System.out.println("MyHandler1 check " + request);
		Handler h = getHandler();
		if(h != null){
			h.operate(request);
		}
	}
}

class MyHandler2 extends AbstractHandler implements Handler{

	@Override
	public void operate(String request) {
		System.out.println("MyHandler2 check " + request);
		Handler h = getHandler();
		if(h != null){
			h.operate(request);
		}
	}
}

class MyHandler3 extends AbstractHandler implements Handler{

	@Override
	public void operate(String request) {
		System.out.println("MyHandler3 operate " + request);
		Handler h = getHandler();
		if(h != null){
			h.operate(request);
		}
	}
}
